<template>
  <div class="app-main-container">
    <div class="df" style="height:100%">
      <div class="w400 sell_left">
        <el-input
          v-model="mealName"
          :placeholder="$t('message.qsr') + $t('message.qcmc')"
          class="input-with-select bottom-speace-style"
          @keyup.enter.native="searchMeals"
        >
          <el-button slot="append" icon="el-icon-search" @click="searchMeals" />
        </el-input>
        <div class="sell_wrap">
          <!-- :class="item.isSelect?'select-style':'normal-style'" -->
          <div class="mt10">
            <div
              v-for="(item, index) in saleTickets"
              :key="index"
              class="card-style"
              @click="selectTick(index)"
            >
              <div class="combo_card_right">
                <div class="card-name">{{ item.name }}</div>
                <div class="card_type">{{ $t('message.sfbz') }}：{{ item.chargeInfo }}</div>
                <div class="specific">
                  <div class="card-first-price secondColor">{{ $t('message.rmbfh') }}{{ item.deposit }}</div>
                  <div class="num-module-style">
                    <div class="sale-style">
                      <span class="num-title-style">{{ $t('message.yishou') }}：</span>
                      <span>{{ item.leaseNum }}</span>
                    </div>
                    <span class="ml10 mr10 placeholder6"> | </span>
                    <div class="surplus-style">
                      <span class="num-title-style">{{ $t('message.sy') }}：</span>
                      <span>{{ item.surplusStock }}</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div>
          <Pagination
            :integrated="false"
            :page="salePagination.page"
            :total="saleTotal"
            :page-size="salePagination.rows"
            @current-change="salePageChange"
          />
        </div>
      </div>
      <div class="separator" />
      <div class="flex1">
        <div class="member_wrap">
          <div class="df">
            <MemberSearch ref="MemberSearchRef" class="flex1" @clear="memberClear" @select="memberSelect" />
            <el-button class="ml10" type="primary" @click="memberClearb">{{ $t('message.sk') }}</el-button>
          </div>
          <div class="df mt20 mb20" style="align-items: center;">
            <template v-if="memberInfo.leaseUserType === '0'">
              <span class="flex1">
                <el-input
                  v-model="memberInfo.nickName"
                  maxlength="50"
                  style="width: 90%;"
                  :placeholder="$t('message.qsr') + $t('message.name')"
                />
              </span>
              <span class="flex1">
                <el-input
                  v-model="memberInfo.phone"
                  maxlength="50"
                  style="width: 90%;"
                  onkeyup="value=value.replace(/[^\d^]/g,'')"
                  :placeholder="$t('message.qsr') + $t('message.sjh')"
                /></span>
            </template>
            <template v-else>
              <span class="flex1">{{ $t('message.name') }}：{{ memberInfo.nickName || '-' }}</span>
              <span class="flex1">{{ $t('message.sjh') }}：{{ memberInfo.phone || '-' }}</span>
            </template>

            <span class="flex1">{{ $t('message.hydj') }}：{{ memberInfo.cardName || '-' }}</span>
            <span class="flex1">{{ $t('message.czje') }}：{{ memberInfo.rechargeMoney || '0.00' }}</span>
            <span class="flex1">{{ $t('message.zsye') }}：{{ memberInfo.giveMoney || '0.00' }}</span>
          </div>
        </div>
        <div class="tables">
          <el-table
            :data="selectedMessage"
            border
            fit
            :header-row-class-name="'table-header'"
            class="doumee-element-table"
          >
            <el-table-column align="center" :label="$t('message.qcmc')" prop="name" min-width="100" show-overflow-tooltip />
            <el-table-column align="center" :label="$t('message.sfbz')" prop="chargeInfo" min-width="120" show-overflow-tooltip />
            <el-table-column align="center" :label="$t('message.yj')" prop="deposit" min-width="80" show-overflow-tooltip />
            <el-table-column align="center" :label="$t('message.mfsjfz')" prop="freeTime" min-width="110" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.freeTime || 0 }}
              </template></el-table-column>
            <!-- <el-table-column align="center" :label="$t('message.num')" width="120">
              <template slot-scope="scope">
                <el-input-number v-model="scope.row.num" size="small" step-strictly :max="scope.row.surplusDay" :step="1" :min="1" :label="$t('message.num')" style="width: 100%" />
              </template>
            </el-table-column> -->
            <el-table-column align="center" :label="$t('message.xiaoji')" prop="deposit" min-width="80" show-overflow-tooltip />
            <el-table-column align="center" :label="$t('message.caozuo')" min-width="80" show-overflow-tooltip>
              <template slot-scope="scope">
                <el-button v-preventReClick type="text" class="secondColor" @click="toDelete(scope.$index)">{{ $t('message.del') }}</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div v-if="selectedMessage.length" class="final_wrap">
          <div class="price_wrap">
            <div class="sum-style">
              <span class="sum-title">{{ $t('message.heji') }} ：</span>
              <span class="sum-value secondColor">{{ totalMoney }}</span>
            </div>
            <!-- <div class="sum-style">
              <span class="sum-title">{{ $t('message.zhje') }} ：</span>
              <span class="sum-value secondColor">{{ totalMoney }}</span>
            </div>
            <div class="sum-style">
              <span class="sum-title">{{ $t('message.yhje') }}：</span>
              <span class="sum-value secondColor">{{ totalMoney }}</span>
            </div> -->
          </div>
          <div class="sub_order">
            <el-input v-model="remark" :placeholder="$t('message.qsr') + $t('message.ddbz')" />
            <el-button v-preventReClick :loading="subLoading" class="ml20" type="primary" @click="createOrder">{{ $t('message.ddjs') }} </el-button>
            <el-button @click="clearOrder">{{ $t('message.clear') }}</el-button>
          </div>
        </div>
      </div>
    </div>
    <orderDialog ref="child" @openSuccess="subLoading = false" @orderSuccess="getList" />
    <!-- <comboConfirm ref="comboConfirm" @confirm="createOrder" /> -->
  </div>
</template>

<script>
import { equipmentList } from '@/api'
import { parseTime } from '@/utils'
import orderDialog from '@/components/order'
// import comboConfirm from '@/components/ComboConfirm'
import Pagination from '@/components/paginations'
import MemberSearch from '@/components/MemberSearch'
export default {
  name: 'ComboSell',
  components: {
    orderDialog,
    // comboConfirm,
    Pagination,
    MemberSearch
  },
  data() {
    return {
      subLoading: false,
      mealName: '',
      cardNum: '',
      dialogMessageTableVisible: false,
      dialogTableVisible: false,
      showMessage: false,
      cardMessage: {},
      date: null,
      zsnum: false,
      saleTickets: [],
      salePagination: {
        page: 1,
        rows: 20
      },
      saleTotal: 0,
      salePage: 1,
      selectedMessage: [],
      project: {
        projectList: []
      },
      param: {
        date: parseTime(new Date())
      },
      remark: '',
      memberInfo: { leaseUserType: '1' }
    }
  },
  computed: {
    // swiper() {
    //   return this.$refs.mySwiper.$swiper
    // },
    totalMoney() {
      let money = 0.0
      this.selectedMessage.forEach(item => {
        money += item.deposit * item.num
      })
      return parseFloat(money).toFixed(2)
    }
  },
  created() {
    this.getMeals()
  },
  methods: {
    memberSelect(item) {
      this.memberInfo = { ...item, leaseUserType: '1' }
    },
    memberClear() {
      this.$refs.MemberSearchRef.clearSearch()
      this.memberInfo = {
        leaseUserType: '1'
      }
    },
    memberClearb() {
      this.$refs.MemberSearchRef.clearSearch()
      this.memberInfo = {
        nickName: this.$t('message.sk'),
        id: '',
        leaseUserType: '0'
      }
    },
    getList() {
      this.memberClear()
      this.clearOrder()
      this.getMeals()
    },
    searchMeals() {
      this.getMeals()
    },
    getMeals() {
      equipmentList({
        param: {
          name: this.mealName,
          status: '1',
          venueId: sessionStorage.getItem('venueId')
        },
        pagination: this.salePagination
      })
        .then(res => {
          if (res.errorCode !== '000000') return
          this.saleTickets = res.recordList
          this.saleTotal = res.totalCount || 0
        })
        .catch(err => {
          console.log(err)
        })
    },
    salePageChange(val) {
      this.salePagination.page = val
      this.getMeals()
    },
    selectTick(index) {
      const item = this.saleTickets[index]
      if (item.surplusStock < 1) {
        return this.$message.warning(this.$t('message.gkcbz'))
      }
      const selectedMessage = [...this.selectedMessage]
      let num = 0
      selectedMessage.forEach((s) => {
        if (s.id === item.id) {
          num = num + 1
        }
      })
      if (num >= item.surplusStock) {
        return this.$message.warning(this.$t('message.gkcbz'))
      }
      this.selectedMessage.push({
        ...item,
        num: 1
      })
    },
    clearOrder() {
      this.selectedMessage = []
    },
    toDelete(index) {
      this.selectedMessage.splice(index, 1)
    },
    createOrder() {
      const { selectedMessage, remark, memberInfo } = this
      if (!memberInfo.id && memberInfo.leaseUserType === '1') {
        return this.$message.warning(this.$t('message.qxxzhyzjs'))
      }
      const config = { selectedMessage, remark, memberInfo: memberInfo }
      this.subLoading = true
      try {
        this.$refs.child.continuePay(config, 'lease')
      } catch (error) {
        this.subLoading = true
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/variables.scss';
.app-main-container {
  height: calc(100% - 40px);
}
.separator{
  margin: 0 20px;
  width: 1px;
  height: 100%;
  background-color: #DFE2E8;
}
.sell_left{
  height: 100%;
  .sell_wrap{
    height: calc(100% - 100px);
    overflow: auto;
  }
}

.tables {
  height: calc(100% - 200px);
  overflow: auto;
}
.card-style {
  cursor: pointer;
  width: 100%;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  padding: 10px;
  box-sizing: border-box;
  border: solid 1px #d8d8d8;
  &:hover {
    border: solid 1px $primaryColor;
  }
  .img-div {
    width: 60px;
    height: 36px;
    margin-right: 10px;
    display: flex;
    img {
      width: 100%;
      border-radius: 2px;
    }
  }
  .combo_card_right{
    flex: 1;
    .card-name {
      margin-bottom: 10px;
      line-height: 14px;
    }
    .card_type{
      margin-bottom: 10px;
      font-size: 12px;
      line-height: 12px;
      color: #666666;
    }
    .time-style {
      color: #666666;
      margin-bottom: 12px;
      font-size: 12px;
      line-height: 12px;
    }
    .specific{
      display: flex;
      line-height: 14px;
      justify-content: space-between;
      .card-first-price {
        font-size: 14px;
      }
      .num-module-style {
        font-size: 12px;
        .sale-style {
          float: left;
        }
        .surplus-style {
          float: right;
        }
        .num-title-style {
          color: #666666;
        }
      }
    }
  }
}

.final_wrap {
  background: #F7F7F7;
  border-radius: 2px;
  padding: 20px;
  .price_wrap{
    display: flex;
    margin-bottom: 20px;
    .sum-style {
      font-size: 16px;
      margin-right: 75px;
      .sum-title {
        color: #222;
      }
      .sum-value {
        font-size: 17px;
        font-weight: 700;
      }
    }
  }
  .sub_order{
    display: flex;
  }
}
.count-style {
  margin-left: 5px;
  margin-right: 5px;
}
.bottom-speace-style {
  margin-bottom: 10px;
}
</style>
